package com.hgts.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgts.common.R;
import com.hgts.pojo.Comment;
import com.hgts.pojo.Likes;
import com.hgts.pojo.User;
import com.hgts.service.ICommentService;
import com.hgts.service.IUserService;
import com.hgts.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凡哥哥
 * @since 2023-05-14
 */
@Slf4j
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    private ICommentService commentService;
    @Autowired
    private IUserService userService;
    /*
     * 评论区
     * */
    @GetMapping("/{bookId}")
    public R<List<Comment>> comment(@PathVariable Long bookId){
        return R.success(commentService.comment(bookId));
    }
    /*
    * 评论区发送
    *
    * */
    @PostMapping
    public R<String> content(@RequestBody Comment comment){
        //通过线程获取用户id
        Integer userId = UserHolder.getUserWx() != null ? UserHolder.getUserWx().getId() : null;
        if (userId != null){
            comment.setUserId(userId);
            //通过User实体类，把nickname的值传递给Comment
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getId,userId);
            User user = userService.getOne(lqw);
            String nickname = user.getNickname();
            comment.setUserNickname(nickname);
            commentService.save(comment);
            return R.success("发送评论成功");
        }
        return R.error("用户未登录");
    }
    /*
    *点赞功能
    * */
    @PutMapping("/like/{commentId}")
    public R<Comment> likeBlog(@PathVariable Long commentId){
        //修改点赞数量
        //通过线程获取用户id
        Integer userID = UserHolder.getUserWx() != null ? UserHolder.getUserWx().getId() : null;
        if (userID != null){
            return commentService.likeComment(commentId);
        }
        return R.error("用户未登录");
    }

}

